package com.evil.common.application.enums.view;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 应用视图类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ApplicationViewTypeEnum {

    /**
     * 应用视图类型
     */
    REPORTING(1, "报表"),
    FILTER(2, "筛选"),
    GRID(3, "宫格"),
    CAROUSEL(4, "轮播"),
    LINK(5, "链接"),
    TEXT(6, "文本"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ApplicationViewTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ApplicationViewTypeEnum isExistById(Integer id) {
        return ApplicationViewTypeEnum.findById(id).orElseThrow(ApplicationViewTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "应用视图类型参数错误");
    }
}
